// pages/teacherName/teacherName.js
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    idx: '',
    idx1: '',
    idx2: '',
    idx3: '',
    isCall: false,
    campusArr: [],
    classArr: [],
    projectArr: [],
    studentList: [],
    class_oneself: [],
    class_hour: [],
    houridx: 0,
    isHidden:true,
    shangkeNum:''
  },
  // 选择课时
  hourPicke(e) {
    console.log(this.data.class_hour);
    this.setData({
      houridx: e.detail.value
    })
  },

  //提交
  subimitInfor() {
  
    this.setData({
      isHidden:false
    })
    console.log(this.data.studentList)
    var shangkeLis = this.data.studentList.filter(item => item.status == 2)
   this.setData({
     shangkeNum: shangkeLis.length
   })
  },
  confirm(){
    var studentList = JSON.parse(JSON.stringify(this.data.studentList));
    studentList.forEach(item => {
      // delete item.color;
      delete item.due_date;
      // item.status = status
    })
    console.log(studentList, this.data.campusArr, this.data.classArr)

    var isStatus = studentList.some((item, index) => {
      return item.status == 2
    })

    if (!isStatus &&
      ((this.data.idx3 === '' && this.data.classArr[this.data.idx2].course_id === 0) || (this.data.idx3 !== '' && this.data.class_oneself[this.data.idx3].course_id === 0))
    ) {
      console.log(this.data.class_hour[this.data.houridx]);
      return wx.showToast({
        title: '请选择学生',
        icon: 'none'
      })
    }
    console.log(this.data.idx3)
   // if (res.confirm) {

      if (this.data.studentList.length > 0) {

        // 正常选课
        if (this.data.idx3 === '') {
          console.log('正常选课')
          var params = {
            token: wx.getStorageSync('token'),
            data: studentList,
            placement_id: this.data.classArr[this.data.idx2].id,
            campus: this.data.campusArr[this.data.idx].id,
            course_id: this.data.classArr[this.data.idx2].course_id,
            class_hour: this.data.class_hour[this.data.houridx]
          }
          // 小课
          if (params.course_id === 0) {
            params.class_hour = this.data.class_hour[this.data.houridx];
            api.fetchApi('teacher/small_class', params).then(res => {
              // console.log(res);
              api.showToast('提交成功').then(() => {
                //清除点名时退出状态
                wx.setStorageSync('isDian', 0)
                setTimeout(() => {
                  wx.reLaunch({
                    url: '../personal/personal',
                  })
                }, 1500)
              })
            })
            console.log(1);
            return;
          }
          wx.reLaunch({
            url: '../teacherName1/teacherName1?studentParams=' + JSON.stringify(params) + '&school=' + this.data.campusArr[this.data.idx].campus_name + '&project=' + this.data.projectArr[this.data.idx1].project_name + '&class1=' + this.data.classArr[this.data.idx2].name,
          })
          //防止中途退出将点名信息存入缓存
          wx.setStorageSync('params', {
            studentParams: JSON.stringify(params),
            school: this.data.campusArr[this.data.idx].campus_name,
            project: this.data.projectArr[this.data.idx1].project_name,
            class1: this.data.classArr[this.data.idx2].name
          })

        } else {
          //我的班级
          console.log('我的班级')
          var params = {
            token: wx.getStorageSync('token'),
            data: studentList,
            placement_id: this.data.class_oneself[this.data.idx3].id,
            campus: this.data.class_oneself[this.data.idx3].campus,
            course_id: this.data.class_oneself[this.data.idx3].course_id,
            class_hour: this.data.class_hour[this.data.houridx]
          }
          // 小课
          if (params.course_id === 0) {
            params.class_hour = this.data.class_hour[this.data.houridx];
            api.fetchApi('teacher/small_class', params).then(res => {
              console.log(res);
              api.showToast('提交成功').then(() => {
                //清除点名时退出状态
                wx.setStorageSync('isDian', 0)
                setTimeout(() => {
                  wx.reLaunch({
                    url: '../personal/personal',
                  })
                }, 1500)
              })
            })
            console.log(2);
            return;
          }
          wx.reLaunch({
            url: '../teacherName1/teacherName1?studentParams=' + JSON.stringify(params) + '&school=' + this.data.class_oneself[this.data.idx3].campus_name + '&project=' + this.data.class_oneself[this.data.idx3].project_name + '&class1=' + this.data.class_oneself[this.data.idx3].name,
          })
          //防止中途退出将点名信息存入缓存
          wx.setStorageSync('params', {
            studentParams: JSON.stringify(params),
            school: this.data.class_oneself[this.data.idx3].campus_name,
            project: this.data.class_oneself[this.data.idx3].project_name,
            class1: this.data.class_oneself[this.data.idx3].name
          })
        }



      } else {
        api.showToast('暂无学生上课', 'http://qnwmfx.laigl.com/iconimg/warn.png');
        return
      }

   // }
  },
  cancel(){
    this.setData({
      isHidden: true
    })
  },

  //点名
  rollCall(e) {
    var student_id = e.currentTarget.dataset.id;
    console.log(student_id)
    var studentList = this.data.studentList;
    console.log(studentList)
    studentList.forEach(item => {
      if (student_id == item.sid) {
        if (item.status == 1) {
          item.status = 2
        } else {
          item.status = 1
        }

      }
    })

    this.setData({
      studentList
    })

  },

  //学校切换
  changeSchool(e) {
    // console.log(e.currentTarget.dataset.id, idx)
    var idx = e.detail.value;
    var nowArr = this.data.campusArr[idx];
    var campus_id = nowArr.id
    console.log(campus_id)
    this.setData({
      idx,
      campus_id
    })
    this.getSchool(campus_id)
    // this.getStudent()
  },

  //项目切换
  changeProject(e) {
    var idx1 = e.detail.value;
    var nowArr = this.data.projectArr[idx1];
    var project_id = nowArr.id
    this.setData({
      idx1,
      project_id
    })
    this.getSchool(this.data.campus_id, project_id)
    // this.getStudent()
  },

  //班级切换
  changeClass(e) {
    var idx2 = e.detail.value;
    console.log(idx2)
    var nowArr = this.data.classArr[idx2];
    var placement_id = nowArr.id
    this.setData({
      idx2,
      placement_id,
      // class_oneself: [],
      idx3: ''
    })

    this.getStudent()
  },
  /**
   * 生命周期函数--监听页面加载
   */

  //获取学生信息
  getStudent() {
    var params = {
      token: wx.getStorageSync('token'),
      campus_id: this.data.campus_id,
      project_id: this.data.project_id,
      placement_id: this.data.placement_id

    }
    api.fetchApi('teacher/student_info', params).then(res => {
      console.log(res);
      this.setData({
        studentList: res.data,
        class_hour: res.class_hour
      })
    })
  },

  //切换我的班级
  changeMyClass(e) {
    var idx3 = e.detail.value;
    var nowArr = this.data.class_oneself[idx3];
    var params = {
      token: wx.getStorageSync('token'),
      placement_id: nowArr.id
    }
    api.fetchApi('teacher/student_info', params).then(res => {
      this.setData({
        studentList: res.data,
        idx3,
        // campusArr: [],
        classArr: [],
        projectArr: [],
        idx: '',
        idx1: '',
        idx2: '',
        // placement_id,
        // campus_name: nowArr.campus_name,
        // project_name:nowArr.project_name,
        // class_name:nowArr.name,
        nowSelfArr: nowArr,
        class_hour: res.class_hour
      })
    })
  },
  //获取校区、项目、班级
  getSchool(id = '', project_id = '', type = 0) {
    api.fetchApi('teacher/getbasic', {
      token: wx.getStorageSync('token'),
      type,
      campus_id: id,
      project_id
    }).then(res => {
      var campusArr = res.campus;
      var classArr = res.class;
      var projectArr = res.project;
      var class_oneself = res.class_oneself;
      //console.log(campusArr, classArr, projectArr)
      this.setData({
        campusArr,
        classArr,
        projectArr,
        class_oneself
      })
    })
  },

  onLoad: function(options) {

    this.getSchool();

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})